<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-input
          v-model="state.listQuery.username"
          style="width: 200px"
          class="filter-item"
          @keyup.enter="handleList"
          placeholder="用户名"
          clearable
      />
      <el-button
          v-waves
          class="filter-item"
          type="primary"
          @click="handleList"
      >
        <el-icon style="vertical-align: middle">
          <Search/>
        </el-icon>
        搜索
      </el-button>
    </div>
    <el-table
        v-loading="state.listLoading"
        :data="state.list"
        :load="state.listLoading"
        border
        max-height="700px"
        :header-cell-style="{'text-align':'center'}"
        style="width: 100%">
      <el-table-column
          label="操作人"
          prop="username"
          align="center"
          width="120">
      </el-table-column>
      <el-table-column
          label="操作描述"
          prop="description"
          align="left"
          width="300">
        <template #default="scope">
          <span style="text-align: left"
                v-text="scope.row.description"/>
        </template>
      </el-table-column>
      <el-table-column
          label="请求类型"
          prop="method"
          align="center"
          width="90">
      </el-table-column>
      <el-table-column
          label="根路径"
          prop="basePath"
          align="center"
          width="180">
      </el-table-column>
      <el-table-column
          label="uri"
          prop="uri"
          align="left"
          width="250">
      </el-table-column>
      <el-table-column
          label="操作时间"
          prop="startTime"
          align="center"
          width="160">
        <template #default="scope">
          <span v-text="formatDateTime(scope.row.startTime)"/>
        </template>
      </el-table-column>
      <el-table-column
          label="消耗"
          prop="spendTime"
          align="center"
          width="90">
        <template #default="scope">
          <span v-if="scope.row.spendTime < 1000"
                v-text="scope.row.spendTime + 'ms'"/>
          <span v-else v-text="(scope.row.spendTime / 1000) + 's'"/>
        </template>
      </el-table-column>
      <el-table-column
          label="状态"
          prop="status"
          align="center"
          width="60">
        <template #default="scope">
          <el-tag
              class="status-tag"
              size="small"
              style="background-color: #b3e19d; width: 30px"
              v-if="scope.row.status != null && scope.row.status === true">
          </el-tag>
          <el-tag
              class="status-tag"
              size="small"
              style="background-color: #eebe77; width: 30px"
              v-else-if="scope.row.status == null">
          </el-tag>
          <el-tag
              class="status-tag"
              size="small"
              style="background-color: #c45656; width: 30px"
              v-else>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
          label="IP"
          prop="ip"
          align="center"
          width="120">
      </el-table-column>
      <el-table-column label="操作" width="210" align="center">
        <template #default="scope">
          <el-button size="small"
                     v-if="scope.row.parameter != null || scope.row.result != null"
                     @click="handleRow(scope.row)"
          >查看参数和结果
          </el-button>
          <el-popconfirm/>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px">
      <el-pagination
          :total="state.total"
          v-model:page="state.listQuery.pageNum"
          v-model:limit="state.listQuery.pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="state.listQuery.pageNum"
          :default-page-size="15"
          :page-sizes="[10, 15, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
    <el-dialog
        title="请求参数和响应结果"
        v-model="state.dialogFormVisible"
    >
      <span class="ml-3 w-35 text-gray-600 inline-flex items-center"
      >请求参数</span>
      <el-input
          class="w-50 m-2"
          v-model="state.parameter"
          :autosize="{minRows: 2, maxRows: 5}"
          type="textarea"
          style="margin-bottom: 50px"
      />
      <span class="ml-3 w-35 text-gray-600 inline-flex items-center"
      >响应结果</span>
      <el-input
          v-model="state.result"
          :autosize="{minRows: 2, maxRows: 20}"
          type="textarea"
      />
    </el-dialog>
  </div>
</template>

<script setup>
import {
  reactive,
  onMounted
} from 'vue'
import {get} from '@/utils/http'
import {apis} from '@/apis'
import moment from 'moment'

const state = reactive({
  tableKey: 0,
  list: [],
  total: 0,
  listLoading: true,
  listQuery: {
    pageNum: 1,
    pageSize: 15,
    username: undefined
  },
  dialogFormVisible: false,
  parameter: '',
  result: ''
})


const handleList = () => {
  state.listLoading = true
  get(apis.ADMIN_ACCESS_LOG, {params: state.listQuery}).then((res) => {
    console.log(res)
    state.list = res.data.content
    state.total = res.data.totalElements
    state.listQuery.pageSize = res.data.size
    state.listLoading = false
  })
}

const formatDateTime = (value) => {
  if (value === null) {
    return value
  }
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
}

const handleSizeChange = (val) => {
  state.listQuery.pageSize = val
  handleList()
}

const handleCurrentChange = (val) => {
  state.listQuery.pageNum = val
  handleList()
}

const handleRow = (row) => {
  state.dialogFormVisible = true
  if (row.parameter == null) {
    state.parameter = ''
  } else {
    state.parameter = JSON.stringify(row.parameter)
  }
  if (row.result == null) {
    state.result = ''
  } else {
    state.result = JSON.stringify(row.result)
  }
}

onMounted(() => {
  handleList()
})

</script>

<style>
.tips-tag {
  margin-left: 30px;
}
</style>


